package com.yantong.controller.order;


import com.yantong.pojo.Centre;
import com.yantong.pojo.vo.IskuaiOrderVo;
import com.yantong.pojo.vo.OrderProductDetailVo;
import com.yantong.service.CentreService;
import com.yantong.annotation.Log;
import com.yantong.constant.OrderStatus;
import com.yantong.constant.PayStatus;
import com.yantong.controller.BaseController;
import com.yantong.result.ResultVo;
import com.yantong.utils.DateUtils;
import com.yantong.utils.IPUtils;
import com.yantong.utils.PageUtils;
import com.yantong.utils.excel.ExcelHelper;
import com.yantong.pojo.*;
import com.yantong.mapper.JuheOrderMapper;
import com.yantong.mapper.JuheOrderRefundMapper;
import com.yantong.mapper.OrderMapper;
import com.yantong.mapper.OrderSplitInfoMapper;
import com.yantong.service.OrderService;
import com.yantong.utils.SysUser;
import com.yantong.pojo.ListMemberParams;
import com.yantong.pojo.Member;
import com.yantong.service.CommanderListService;
import com.yantong.pay.yfz.pay.YFZPayutil;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.*;

/**
 * 订单管理
 *
 * @author kanghaixin
 * @date 2018/9/7.15:39
 */

@Controller
@RequestMapping("/order/manage")
public class OrderController extends BaseController {
	private static final String prefix = "order";
	private static final Logger logger = LoggerFactory.getLogger(OrderController.class);
	@Autowired
	private OrderService orderService;
	@Autowired
	private OrderMapper orderMapper;
	@Autowired
	private CommanderListService commanderListService;
	@Autowired
	private OrderSplitInfoMapper orderSplitInfoMapper;
	@Autowired
	private JuheOrderRefundMapper juheOrderRefundMapper;
	@Autowired
	private JuheOrderMapper juheOrderMapper;
	@Autowired
	private CentreService centreService;
	@Value("${juhe.notify}")
	private String juhenotifyUrl;


	/**
	 * 订单列表
	 *
	 * @param model
	 * @return
	 */
	@RequiresPermissions("order:manage:manage")
	@GetMapping()
	public String orders(Model model) {
		model.addAttribute("payStatusList", PayStatus.getPayStatusList());
		model.addAllAttributes(getCentreIdModelInfo());
		return prefix + "/order";
	}


	/**
	 * 订单列表数据
	 *
	 * @param params
	 * @param offset
	 * @param limit
	 * @return
	 */
	@ResponseBody
	@RequiresPermissions(value = {"order:manage:manage",
			"order:waitPay:waitPay", "order:waitSend:waitSend",
			"order:waitReceive:waitReceive", "order:success:success", "order:closed:closed"}, logical = Logical.OR)
	@GetMapping("/list")
	public PageUtils<Order> listOrders(ListOrderParams params, int offset, int limit) {
		Long teamId = getUserTeamId();
		if (null != teamId) {
			params.setTmid(teamId);
		}
		return orderService.listOrders(params, offset, limit);
	}

	/**
	 * 订单商品列表页面
	 *
	 * @param model
	 * @return
	 */
	@RequiresPermissions("order:manage:ordProductlistByParams")
	@GetMapping("/orderProductPage")
	public String orderProductPage(Model model) {
		model.addAttribute("payStatusList", PayStatus.getPayStatusList());
		Map<String, Object> map = getCentreIdModelInfo();
		ListMemberParams listMemberParams = new ListMemberParams();
		if (map.get("centreId") != null) {
			listMemberParams.setCentreId(new Long(map.get("centreId").toString()));
		}
		List<Member> teams = commanderListService.listCommanders(listMemberParams, new Member());
		model.addAllAttributes(map);
		model.addAttribute("teams", teams);
		return prefix + "/orderproduct";
	}

	/**
	 * 条件查询订单商品列表
	 *
	 * @param params
	 * @param offset
	 * @param limit
	 * @return
	 */
	@ResponseBody
	@RequiresPermissions("order:manage:ordProductlistByParams")
	@GetMapping("/OrderProductVoList")
	public PageUtils<OrderProductDetailVo> ordProductlistByParams(OrderProductDetailVo params, int offset, int limit) {
		Long teamId = getUserTeamId();
		if (null != teamId) {
			params.setTmid(teamId.toString());
		}
		return orderService.ordProductlistByParams(params, offset, limit);
	}


	@RequiresPermissions(value = {"order:manage:ordProductlistByParams"})
	@GetMapping("/exportorderproduct")
	public void exportOrderProductlistByParams(OrderProductDetailVo params, HttpServletResponse response) {
		List<List<Object>> resultData = new ArrayList<>();
		String[] head = {"订单ID", "订单号", "商品名称", "购买数量", "原价", "应付", "所属门店", "门店自提/收货地址", "下单人", "收货人", "收货人手机",
				"商品小计", "运费", "下单时间", "支付状态", "支付时间", "订单状态"};
		resultData.add(0, Arrays.asList(head));
		List<OrderProductDetailVo> list = orderMapper.ordProductlistByParams1(params);
		if (CollectionUtils.isNotEmpty(list)) {
			for (OrderProductDetailVo o : list) {
				if (o == null) {
					continue;
				}
				List<Object> row = new ArrayList<>();
				row.add(o.getId());
				row.add(o.getOrdersn());
				row.add(o.getProductName());
				row.add(o.getTotal());
				row.add(o.getProductprice());
				row.add(o.getMarketprice());
				row.add(o.gettXiaoqu());
				row.add(o.getSaddress());
				row.add(o.getNickname());
				row.add(o.getSname());
				row.add(o.getMobile());
				row.add(o.getPayprice());
				row.add(o.getDispatchprice());
				row.add(o.getCreateTime());
				row.add(PayStatus.get(Integer.parseInt(o.getPaystatus())));
				row.add(o.getPayTime());
				row.add(OrderStatus.get(Integer.parseInt(o.getStatus())));
				resultData.add(row);
			}
		}
		String fileName = "订单商品记录_" + DateUtils.format(new Date(), "yyyyMMddHHmmss");
		try {
			ExcelHelper.exportExcel(response, fileName, "订单商品记录", resultData);
		} catch (IOException e) {
			logger.info("export order list error:{}", e.getMessage());
		}
	}

	/**
	 * 得到平台商品的总成本，总利益
	 *
	 * @param params
	 * @return
	 */
	@ResponseBody
	@GetMapping("/getBaseProductMoneyTotal")
	public ResultVo getBaseProductMoneyTotal(OrderProductDetailVo params) {
		ResultVo resultVo = new ResultVo();
		resultVo.put("vo", orderService.getBaseProductMoneyTotal(params));
		return resultVo;
	}

	;

	/**
	 * 订单商品列表页面
	 *
	 * @param model
	 * @return
	 */
	@RequiresPermissions("order:manage:orderProductBasePage")
	@GetMapping("/orderProductBasePage")
	public String orderProductBasePage(Model model) {
		return prefix + "/baseordProduct";
	}

	/**
	 * 条件查询平台订单商品列表
	 *
	 * @param params
	 * @param offset
	 * @param limit
	 * @return
	 */
	@RequiresPermissions("order:manage:orderProductBasePage")
	@ResponseBody
	@GetMapping("/ordProductBaselistByParams")
	public PageUtils<OrderProductDetailVo> ordProductBaselistByParams(OrderProductDetailVo params, int offset, int limit) {
		return orderService.ordProductBaselistByParams(params, offset, limit);
	}


	@RequiresPermissions(value = {"order:closed:export"})
	@GetMapping("/export")
	public void exportOrders(ListOrderParams params, HttpServletResponse response) {
		List<List<Object>> resultData = new ArrayList<>();
		String[] head = {"订单ID", "订单号", "支付单号", "商户单号", "所属门店", "门店自提/收货地址", "下单人", "收货人", "收货人手机", "门牌号",
				"订单总额", "支付金额", "余额抵扣", "运费", "下单时间", "支付状态", "支付时间", "门店名称", "门店佣金", "配送员姓名", "配送员电话", "商品名称", "规格名称",
				"商品数量", "应收价格"};
		resultData.add(0, Arrays.asList(head));

		List<Order> list = orderService.listOrders(params);
		if (CollectionUtils.isNotEmpty(list)) {
			for (Order o : list) {
				if (o == null) {
					continue;
				}
				List<Object> row = new ArrayList<>();
				row.add(o.getId());
				row.add(o.getOrdersn());
				row.add(o.getPayno());
				row.add(o.getPayMerchantNo());
				row.add(o.gettXiaoqu());
				row.add(o.getSaddress());
				row.add(o.getNickname());
				row.add(o.getSname());
				row.add(o.getMobile());
				row.add(o.getRemark());
				row.add(o.getOrderprice());
				row.add(o.getPayprice());
				row.add(o.getDeductcredit2());
				row.add("");
				row.add(o.getCreateTime());
				row.add(PayStatus.get(o.getPaystatus()));
				row.add(o.getPayTime());
				row.add(o.gettXiaoqu());
				row.add("");
				row.add("");
				row.add("");
				if (CollectionUtils.isNotEmpty(o.getOrderProducts())) {
					List<OrderProduct> ops = o.getOrderProducts();
					List<Object> dr = row;
					for (OrderProduct op : ops) {
						dr.add(op.getProductName());
						dr.add(op.getOptionname());
						dr.add(op.getTotal());
						dr.add(op.getPrice());
					}
					resultData.add(dr);
				}

			}
		}
		String fileName = "订单记录_" + DateUtils.format(new Date(), "yyyyMMddHHmmss");
		try {
			ExcelHelper.exportExcel(response, fileName, "订单记录", resultData);
		} catch (IOException e) {
			logger.info("export order list error:{}", e.getMessage());
		}
	}


	/**
	 * 后台关闭订单
	 *
	 * @param orderId
	 * @param request
	 * @return
	 */
	@Log(module = "订单管理", operation = "关闭订单")
	@ResponseBody
	@RequiresPermissions(value = {"order:manage:close", "order:waitPay:close", "order:success:close"}, logical = Logical.OR)
	@PostMapping("/closeOrder")
	public ResultVo closeOrder(Long orderId, HttpServletRequest request) {
		if (orderId == null) {
			return ResultVo.error();
		}
		if (orderService.closeOrder(orderId, IPUtils.getIpAddr(request), "后台关闭订单") < 1) {
			return ResultVo.error();
		}
		return ResultVo.ok();
	}

	/**
	 * 后台确认发货
	 *
	 * @param orderId
	 * @return
	 */
	@Log(module = "订单管理", operation = "确认发货")
	@ResponseBody
	@PostMapping("/sendGoods")
	@RequiresPermissions(value = {"order:manage:sendGoods", "order:waitSend:sendGoods"}, logical = Logical.OR)
	public ResultVo sendGoods(Long orderId) {
		if (orderId == null) {
			return ResultVo.error();
		}
		if (orderService.sendGoods(orderId) < 1) {
			return ResultVo.error();
		}
		return ResultVo.ok();
	}


	/**
	 * 后台确认收货
	 *
	 * @param orderId
	 * @return
	 */
	@Log(module = "订单管理", operation = "确认收货")
	@ResponseBody
	@PostMapping("/confirmReceived")
	@RequiresPermissions(value = {"order:manage:confirmReceived", "order:waitReceive:confirmReceived"}, logical = Logical.OR)
	public ResultVo confirmReceived(Long orderId) {
		if (orderId == null) {
			return ResultVo.error();
		}
		if (orderService.confirmReceived(orderId) < 1) {
			return ResultVo.error();
		}
		return ResultVo.ok();
	}

	/**
	 * 后台订单退款申请
	 *
	 * @param orderId
	 * @return
	 */
	@Log(module = "订单管理", operation = "后台订单退款申请")
	@ResponseBody
	@PostMapping("/refundOrder")
	@RequiresPermissions(value = {"order:manage:refund", "order:waitSend:refund"}, logical = Logical.OR)
	public ResultVo refundOrder(Long orderId) {
		if (orderId == null) {
			return ResultVo.error();
		}
		if (orderService.refundOrder(orderId, getUserId(), getNikeName()) < 1) {
			return ResultVo.error();
		}
		return ResultVo.ok();
	}

	/**
	 * 上传物流单图片
	 *
	 * @param order
	 * @return
	 */
	@Log(module = "订单管理", operation = "上传物流单图片")
	@ResponseBody
	@PostMapping("/uploadLogisticsImage")
	public ResultVo uploadLogisticsImage(Order order) {
		if (order.getId() == null) {
			return ResultVo.error();
		}
		if (orderService.sendGoods(order.getId()) < 1) {
			return ResultVo.error("发货失败");
		}
		return ResultVo.ok();
	}


//	/**
//	 * 后台单品申请退款
//	 *
//	 * @return
//	 */
//	@Log(module = "订单管理", operation = "后台单品申请退款")
//	@ResponseBody
//	@PostMapping("/refundSingle")
//	@RequiresPermissions(value = {"order:manage:refund", "order:waitSend:refund"}, logical = Logical.OR)
//	public ResultVo singleRefundOrder(Long orderProductId) {
//		if (orderProductId == null) {
//			return ResultVo.error();
//		}
//		if (orderService.cancelOrderProduct(orderProductId) < 1) {
//			return ResultVo.error();
//		}
//      /*  if (orderService.singleRefundOrder(orderProductId, getUserId(), getNikeName())< 1) {
//            return ResultVo.error();
//        }*/
//		return ResultVo.ok();
//	}

	/**
	 * 订单详情
	 *
	 * @param model
	 * @param orderId
	 * @return
	 */
	@GetMapping("/detail/{orderId}")
	public String detail(Model model, @PathVariable Long orderId) {
		model.addAttribute("order", orderService.getByKey(orderId));
		return prefix + "/detail";
	}

	@Log(module = "订单管理", operation = "编辑订单")
	@ResponseBody
	@PostMapping("/update")
	public ResultVo update(Order order) {
		if (order == null || order.getId() == null) {
			return ResultVo.error();
		}
		if (orderService.update(order) < 1) {
			return ResultVo.error();
		}
		return ResultVo.ok();
	}

	/**
	 * 待付款订单列表页面
	 *
	 * @return
	 */
	@GetMapping("/waitPay")
	@RequiresPermissions("order:waitPay:waitPay")
	public String waitPay(Model model) {
		model.addAllAttributes(getCentreIdModelInfo());
		return prefix + "/waitPay";
	}


	/**
	 * 待发货订单列表页面
	 *
	 * @return
	 */
	@GetMapping("/waitSend")
	@RequiresPermissions("order:waitSend:waitSend")
	public String waitSendGoods(Model model) {
		model.addAllAttributes(getCentreIdModelInfo());
		return prefix + "/waitSend";
	}


	/**
	 * 待收货订单列表页面
	 *
	 * @return
	 */
	@GetMapping("/waitReceive")
	@RequiresPermissions("order:waitReceive:waitReceive")
	public String waitReceive(Model model) {
		model.addAllAttributes(getCentreIdModelInfo());
		return prefix + "/waitReceive";
	}

	/**
	 * 已收货订单列表页面
	 *
	 * @return
	 */
	@GetMapping("/receivedGoods")
	@RequiresPermissions("order:receivedGoods:list")
	public String receivedGoods(Model model) {
		model.addAllAttributes(getCentreIdModelInfo());
		return prefix + "/receivedGoods";
	}


	/**
	 * 已完成订单列表页面
	 *
	 * @return
	 */
	@GetMapping("/success")
	@RequiresPermissions("order:success:success")
	public String orderSuccess(Model model) {
		model.addAllAttributes(getCentreIdModelInfo());
		return prefix + "/orderSuccess";
	}

	/**
	 * 已关闭订单列表页面
	 *
	 * @return
	 */
	@GetMapping("/closed")
	@RequiresPermissions("order:closed:closed")
	public String closed(Model model) {
		model.addAllAttributes(getCentreIdModelInfo());
		return prefix + "/closed";
	}

	/**
	 * 导出平台商品订单
	 */
	@RequiresPermissions(value = {"order:manage:exportsBaseProductOrder"})
	@GetMapping("/exportsBaseProductOrder")
	public void exportsBaseProductOrder(OrderProductDetailVo params, HttpServletResponse response) {
		List<List<Object>> resultData = new ArrayList<>();
		String[] head = {"序号", "订单号", "用户名", "电话", "站点", "商品名称", "订单商品价格", "数量", "订单商品总计", "平台现价",
				"平台成本价", "站点售价", "供应商", "支付状态", "状态"};
		resultData.add(0, Arrays.asList(head));

		List<OrderProductDetailVo> list = orderService.ordProductBaselistByParamsExp(params);
		if (CollectionUtils.isNotEmpty(list)) {
			int indexer = 1;
			for (OrderProductDetailVo o : list) {
				if (o == null) {
					continue;
				}
				String opstatus = "";
				if (o.getOpstatus().equals("0")) {
					opstatus = "待付款";
				} else if (o.getOpstatus().equals("1")) {
					opstatus = "已付款";
				} else if (o.getOpstatus().equals("2")) {
					opstatus = "已发货";
				} else if (o.getOpstatus().equals("3")) {
					opstatus = "已收获";
				} else if (o.getOpstatus().equals("4")) {
					opstatus = "待退款";
				} else if (o.getOpstatus().equals("5")) {
					opstatus = "售后中";
				} else if (o.getOpstatus().equals("6")) {
					opstatus = "已完成";
				} else if (o.getOpstatus().equals("10")) {
					opstatus = "退款成功";
				} else if (o.getOpstatus().equals("-1")) {
					opstatus = "已取消";
				}
				List<Object> row = new ArrayList<>();
				row.add(indexer);
				row.add(o.getOrdersn());
				row.add(o.getNickname());
				row.add(o.getMobile());
				row.add(o.getCentreName());
				row.add(o.getProductName() == null ? "" : o.getProductName());
				row.add(o.getMarketprice());
				row.add(o.getTotal());
				row.add(o.getPrice());
				row.add(o.getBmarketprice() == null ? "0" : o.getBmarketprice());
				row.add(o.getBcostprice() == null ? "0" : o.getBcostprice());
				row.add(o.getSuggestsaleprice() == null ? "0" : o.getSuggestsaleprice());
				row.add(o.getSupname() == null ? "" : o.getSupname());
				row.add(PayStatus.get(Integer.parseInt(o.getOppayStatus())));
				row.add(opstatus);
				resultData.add(row);
				indexer++;
			}
		}
		String fileName = "平台商品订单_" + DateUtils.format(new Date(), "yyyyMMddHHmmss");
		try {
			ExcelHelper.exportExcel(response, fileName, "平台商品订单", resultData);
		} catch (IOException e) {
			logger.info("export order list error:{}", e.getMessage());
		}
	}


	/**
	 * 订单列表(普通订单)
	 *
	 * @param model
	 * @return
	 */
	@RequiresPermissions("order:manage:manage")
	@GetMapping("/orderPutong")
	public String orderPutong(Model model) {
		model.addAttribute("payStatusList", PayStatus.getPayStatusList());
		model.addAllAttributes(getCentreIdModelInfo());
		return prefix + "/order_putong";
	}

	/**
	 * 订单列表(预售订单)
	 *
	 * @param model
	 * @return
	 */
	@RequiresPermissions("order:manage:manage")
	@GetMapping("/orderYushou")
	public String orderYushou(Model model) {
		model.addAttribute("payStatusList", PayStatus.getPayStatusList());
		model.addAllAttributes(getCentreIdModelInfo());
		return prefix + "/order_yushou";
	}

	/**
	 * 订单列表(拼团订单)
	 *
	 * @param model
	 * @return
	 */
	@RequiresPermissions("order:manage:manage")
	@GetMapping("/orderPintuan")
	public String orderPintuan(Model model) {
		model.addAttribute("payStatusList", PayStatus.getPayStatusList());
		model.addAllAttributes(getCentreIdModelInfo());
		return prefix + "/order_pintuan";
	}

	/**
	 * 订单列表(秒杀订单)
	 *
	 * @param model
	 * @return
	 */
	@RequiresPermissions("order:manage:manage")
	@GetMapping("/orderMiaosha")
	public String orderMiaosha(Model model) {
		model.addAttribute("payStatusList", PayStatus.getPayStatusList());
		model.addAllAttributes(getCentreIdModelInfo());
		return prefix + "/order_miaosha";
	}

	/**
	 * 订单列表(预约订单)
	 *
	 * @param model
	 * @return
	 */
	@RequiresPermissions("order:manage:manage")
	@GetMapping("/orderYuyue")
	public String orderYuyue(Model model) {
		model.addAttribute("payStatusList", PayStatus.getPayStatusList());
		model.addAllAttributes(getCentreIdModelInfo());
		return prefix + "/order_yuyue";
	}

	/**
	 * 订单列表(积分兑换订单)
	 *
	 * @param model
	 * @return
	 */
	@RequiresPermissions("order:manage:manage")
	@GetMapping("/orderJifen")
	public String orderJifen(Model model) {
		model.addAttribute("payStatusList", PayStatus.getPayStatusList());
		model.addAllAttributes(getCentreIdModelInfo());
		return prefix + "/order_jifen";
	}

	/**
	 * 分配配送员页面
	 */
	@GetMapping("/allotDeliveryman/{id}")
	@RequiresPermissions("order:waitSend:allot")
	public String allotDeliveryman(@PathVariable String id, Model model) {
		model.addAttribute("orderId", id);
		return prefix + "/allotDeliveryman";
	}

//	/**
//	 * 后台分配配送员
//	 *
//	 * @param orderId
//	 * @return
//	 */
//	@Log(module = "订单管理", operation = "分配配送员")
//	@ResponseBody
//	@PostMapping("/allot")
//	@RequiresPermissions(value = {"order:waitSend:allot"})
//	public ResultVo allot(Long orderId, Long deliverymanUserId, String deliverymanName, String deliverymanPhone) {
//		if (orderId == null) {
//			return ResultVo.error();
//		}
//		if (deliverymanUserId == null) {
//			return ResultVo.error();
//		}
//		if (deliverymanName == null || "".equals(deliverymanName)) {
//			return ResultVo.error();
//		}
//		if (deliverymanPhone == null || "".equals(deliverymanPhone)) {
//			return ResultVo.error();
//		}
//		return orderService.allotDeliveryman(orderId, deliverymanUserId, deliverymanName, deliverymanPhone);
//	}

	/**
	 * 聚合订单列表
	 *
	 * @return
	 */
	@RequiresPermissions("juheOrder:manage:manage")
	@GetMapping("/tojuheOrder")
	public String tojuheList() {
		return prefix + "/juheOrder";
	}

	/**
	 * 聚合订单列表
	 *
	 * @return
	 */
	@RequiresPermissions("juheOrder:manage:manage")
	@ResponseBody
	@GetMapping("/juheOrderList")
	public PageUtils<JuheOrder> juheList(String ordersn, Integer payType, Integer paystatus, Date startTime, Date endTime, int offset, int limit) {
		return orderService.juheOrdersList(null,getUserTeamId(), getUserCentreId(), ordersn, paystatus, payType, startTime, endTime, offset, limit);
	}

	/**
	 * 上传物流单跳转页面
	 *
	 * @param model
	 * @param orderId
	 * @return
	 */
	@GetMapping("/uploadLogistics/{orderId}")
	public String uploadLogistics(Model model, @PathVariable Long orderId) {
		model.addAttribute("order", orderService.getByKey(orderId));
		return prefix + "/uploadLogistics";
	}


	/**
	 * 分账信息
	 *
	 * @param model
	 * @param outTradeNo
	 * @return
	 */
	@GetMapping("/splitAccountInfo")
	public String splitAccountInfo(Model model, String outTradeNo) {
//        model.addAttribute("order", orderService.getByKey(orderId));
		SysUser sysUser = getUser();
		Long centreId = sysUser.getCentreId();
		Long teamId = sysUser.getTeamId();

		List<OrderSplitInfo> orderSplitInfoList = orderSplitInfoMapper.selectByOutTradeNo(outTradeNo);
		List<OrderSplitInfo> newList = new ArrayList<>();
		for (OrderSplitInfo m : orderSplitInfoList) {
			if (teamId != null) {
				if (!teamId.equals(m.getTeamId())) {
					continue;
				}
			} else {
				if (centreId != null && !centreId.equals(m.getCentreId())) {
					continue;
				}
			}
			if (m.getServiceCharge() == null) {
				m.setServiceCharge(BigDecimal.ZERO);
			}
			if (m.getYfzServiceCharge() == null) {
				m.setYfzServiceCharge(BigDecimal.ZERO);
			}
			if (m.getStatus() != null) {
				//状态0-未发放 1-已发放 2已退款 3已售后退款
				String str = "";
				if (m.getStatus() == 0) {
					str = "未发放";
				} else if (m.getStatus() == 1) {
					str = "已发放";
				} else if (m.getStatus() == 2) {
					str = "已退款";
				} else if (m.getStatus() == 3) {
					str = "已售后退款";
				}
				m.setReturnCode(str);
			}
			newList.add(m);
		}
		model.addAttribute("list", newList);
		return prefix + "/splitAccountInfo";
	}

	/**
	 * 聚合支付退款详情
	 */
	@GetMapping("/refundDetail")
	public String refundDetail(Model model, String refundNo) {
		JuheOrderRefund juheOrderRefund = juheOrderRefundMapper.selectByRefundNo(refundNo);
		model.addAttribute("juheOrderRefund", juheOrderRefund);
		return prefix + "/juheOrderRefundInfo";
	}

	/**
	 * 聚合支付退款审核通过
	 */
	@GetMapping("/jhOrder/audit/success")
	@ResponseBody
	public ResultVo auditSuccess(Long juheOrderRefundId) throws Exception {
		JuheOrderRefund juheOrderRefund = juheOrderRefundMapper.selectByPrimaryKey(juheOrderRefundId);
		if (juheOrderRefund == null) {
			return ResultVo.error();
		}
		JuheOrder juheOrder = juheOrderMapper.selectOrderByOutTradeNo(juheOrderRefund.getOutTradeNo());
		if (juheOrder == null) {
			return ResultVo.error();
		}
		List<OrderSplitInfo> list = orderSplitInfoMapper.selectByOutTradeNo(juheOrder.getOutTradeNo());
		Centre centre = centreService.getByKey(juheOrder.getCenterId());
		YFZPayutil.refundApply(juheOrderRefund.getApplyPrice(), juheOrder.getOutTradeNo(), centre, juheOrderRefund.getRefundNo(), juhenotifyUrl,list.get(0).getSplitResultInfoList());
		return ResultVo.ok();
	}

	/**
	 * 聚合支付退款拒绝原因填写页面
	 */
	@GetMapping("/juheOrderRefundRefuse")
	public String juheOrderRefundRefuse(Model model, Long juheOrderRefundId) {
		model.addAttribute("id", juheOrderRefundId);
		return prefix + "/juheOrderRefundRefuse";
	}

	/**
	 * 聚合支付退款审核拒绝
	 */
	@GetMapping("/jhOrder/audit/fail")
	@ResponseBody
	@Transactional(rollbackFor = Exception.class)
	public ResultVo auditFail(Long juheOrderRefundId, String content) {
		JuheOrderRefund juheOrderRefund = juheOrderRefundMapper.selectByPrimaryKey(juheOrderRefundId);
		if (juheOrderRefund == null) {
			return ResultVo.error();
		}
		JuheOrder juheOrder = juheOrderMapper.selectOrderByOutTradeNo(juheOrderRefund.getOutTradeNo());
		if (juheOrder == null) {
			return ResultVo.error();
		}
		SysUser sysUser = getUser();
		juheOrderRefund.setContent(content);
		juheOrderRefund.setRefundTime(new Date());
		juheOrderRefund.setRefundMid(sysUser.getUserId());
		juheOrderRefund.setRefundName(sysUser.getName());
		juheOrderRefund.setStatus(2);
		juheOrderRefundMapper.updateByPrimaryKeySelective(juheOrderRefund);

		juheOrder.setPaystatus(5);
		juheOrder.setRefundTime(new Date());
		juheOrderMapper.updateById(juheOrder);
		return ResultVo.ok();
	}
}
